<!doctype html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>数据上报管理</title>
        <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
        <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
        <script type="text/javascript" src="/webapp/js/common/config.js"></script>
        <style type="text/css">
            #taskList,#errorRecordText,#channelReason ,#channelS{
                border: 1px solid #959595;
            }
            #taskList tr th,#channelReason tr th,#errorRecord tr th{
                background-color: #6ecac5;
            }
            #channelReason tr td , #taskList tr td ,#errorRecord tr th{
                border: 1px #d6d6d6 dashed;
                font-size: 15px;
                text-align: center;
                height: 30px;
                font-family:"微软雅黑";
            }

        </style>
    </head>
    <body>
        <div class="easyui-container-fluid">
            <div class="easyui-panel" style="border: none;">
                <div class="nav" id="nav"></div>
                <div class="easyui-panel" style="border: none;">
                    <form id="ff">
                        <div style="margin-bottom:20px">

                            <label class="item-label">报告年份：</label>
                            <input type="text" id="year" name="reportYear" class="easyui-combobox" style="width: 100px;"/>
                            <span id="monthLabel">
	                        <label class="item-label">报告月份：</label>
	                        <input id="month" name="reportMonth" type="text" class="easyui-combobox" style="width: 100px;"/>
                        </span>
                            <span id="weekLabel">
                            <label class="item-label">报告周：</label>
                        	<input type="text" id="week" name="reportWeek" class="easyui-combobox" style="width: 100px;"/>
                        </span>
                            <label class="item-label">渠道：</label>
                            <input type="text" id="channel" name="channelCode" class="easyui-combobox" style="width: 100px;"/>
                            <label class="item-label">导入状态：</label>
                            <select name="importStatus" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:100px;">
                                <option value="">-请选择-</option>
                                <option value="SUCCESS">导入成功</option>
                                <option value="WAITING">待导入(旧)</option>
                                <option value="WAITING_NEW">待导入(新)</option>
                                <option value="FAIL">导入失败</option>
                                <option value="RUNNING">分析中</option>
                                <option value="NONE">无记录</option>
                            </select>
                            <label class="item-label">报告总数：</label>
                            <input id="countFlag" class="easyui-switchbutton" style="width:80px;height:30px;"  >
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left:40px;" onclick="searchBy()">查询</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left: 40px;" onclick="channelStatistics()">统计</a> <!--统计渠道状态-->
                            <!--<a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left:40px;" onclick="createAll()">一键创建</a>-->
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left: 40px;" onclick="taskList()">任务列表</a>

                            <a href="javascript:void(0);" class="easyui-linkbutton c6" style="width:80px;margin-left: 40px;" onclick="errorRecord()">异常记录</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left: 40px;" onclick="cq()">渠道核查</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton c8" style="width:80px;margin-left: 40px;" onclick="cxs()">渠道细审</a>
                        </div>
                    </form>
                </div>
                <div class="table-wrapper">
                    <table id="datagrid" style="width:100%" data-options="fitColumns:true,scrollbarSize:0">
                        <thead>
                            <tr>
                                <th data-options="field:'channelName',align:'center'" width="40">渠道</th>
                                <th data-options="field:'code',align:'center'" width="60">报告编码</th>
                                <th data-options="field:'reportType',align:'center',formatter:reportTypeFormatter" width="40">报告类型</th>
                                <th data-options="field:'reportYear',align:'center'" width="40">报告年份</th>
                                <th data-options="field:'reportMonth',align:'center'" width="40">报告月份</th>
                                <th data-options="field:'reportWeek',align:'center'" width="40">报告周</th>
                                <th data-options="field:'reportBeginDate',align:'center'" width="60">数据开始时间</th>
                                <th data-options="field:'reportEndDate',align:'center'" width="60">数据结束时间</th>
                                <th data-options="field:'reportCount',align:'center'" width="40">报告总数</th>
                                <th data-options="field:'importStatus',align:'center',formatter:importFormatter" width="40">上传状态</th>
                                <th data-options="field:'reportStatus',align:'center',formatter:reportStatusFormatter" width="40">报告状态</th>
                                <th data-options="field:'action',formatter:actions,align:'center'" width="90">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <div id="uploadChannel" class="easyui-window upload-wrapper" title="上传结果" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1200px;height: 430px;padding:10px;">
                <div>
                    <li>信息为：</li>
                    <span id="uploadMessage"></span></div>
                <div style="margin-left: 550px; ">
                    <button class="easyui-linkbutton c8" style="width: 100px;margin-top: 130px;" onclick="clickHandler()">确定</button>
                </div>
            </div>

            <div id="taskListText" class="easyui-window upload-wrapper" title="任务列表" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:900px;height: 460px;padding:10px;">
                <input type="button" class="easyui-linkbutton" onclick="upload_refresh()" value="刷新" style="height: 32px;width:60px;background:#cde5fd"/>
<!--                <input type="button" class="easyui-linkbutton" onclick="AiReAnalysis()" value="修改AI异常" style="margin-left: 10px;height: 32px;width:100px;background:#cde5fd"/>-->
<!--                <input type="button" class="easyui-linkbutton" onclick="ai_refresh()" value="刷新AI" style="height: 32px;width:60px;background:#cde5fd"/>-->
                <table id="taskList" style="width: 100%;" cellspacing="0" >
                    <thead>
                        <tr>
                            <th style='width:4%'>#</th>
                            <th style='width:8%'>年</th>
                            <th style='width:6%'>月</th>
                            <th style='width:6%'>周</th>
                            <th style='width:11%'>报告类型</th>
                            <th style='width:17%'>渠道</th>
                            <th style='width:18%'>文件名</th>
                            <th style='width:12%'>导入状态</th>
                            <th style='width:5%'>操作</th>
                        </tr>
                    </thead>
                    <tbody id = "taskListBody"></tbody>
                </table>
            </div>
            <div id="errorRecordText" class="easyui-window upload-wrapper" title="异常记录" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:1000px;height: 500px;padding:10px;">
                <table id="errorRecord" style="width: 100%;" cellspacing="0" ></table>
            </div>
        </div>
        <div id="channelS" class="easyui-window upload-wrapper" title="渠道统计" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:900px;height: 740px;padding:10px;">
            <table id="channelReason" style="width: 100%;" cellspacing="0" ></table>
        </div>
        <div id="reasonText" class="easyui-window upload-wrapper" title="原因记录" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:400px;height: 200px;padding:10px;">
            <label class="item-label">备注原因：</label>
            <input type="text" id="reason" name="channelCode" class="easyui-textbox" style="width: 200px;"/>
            <div >
                <button class="easyui-linkbutton c8" style="width: 100px; margin-left: 100px;margin-top:50px; " onclick="clickReason()">确定</button>
            </div>
        </div>
    </body>
    <script type="text/javascript">
        var dId="";
        $(function () {
            setNav();
            initMonthWeek("monthLabel", "weekLabel");
            initChannelSelect("channel");
            var height = $(window).height();
            var nav_height = $("#nav")._outerHeight();
            var form_height = $("#ff").height();

            $("#datagrid").css("max-height", (height - nav_height - form_height - 20));
            $("#datagrid").datagrid({
                pagination: true,
                rownumbers: true,
                singleSelect: true,
                checkOnSelect: false,
                toolbar: [{
                    text: "添加报告",
                    handler: function () {
                        window.parent.addTab("添加报告", "/webapp/pages/channel/upload/add.html");
                    }
                }],
                onBeforeSelect: function () {
                    return false;
                }
            }).datagrid("getPager").pagination({
                onSelectPage: searchBy
            });
            searchBy(1, 100);
            aRecords("报告列表","/webapp/pages/channel/upload")
        });

        function actions(val, row) {
            return "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
                "style='width:90px;' onclick='manage(\"" + row.id + "\")'>上传文件</a>&nbsp;&nbsp;" +
                "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' " +
                "style='width:60px;' onclick='edit(\"" + row.id + "\")'>修改</a>&nbsp;&nbsp;" +
                "<a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:60px;' onclick='disableOne(\"" + row.id + "\")'>删除</a>";
        }

        function disableOne(id, entityStatus) {
            $.messager.confirm("提示", "您确定删除该记录吗？", function (flag) {
                if (flag) {
                    $.ajax({
                        url: config.url + "/dataReport/" + id,
                        type: 'DELETE',
                        xhrFields: {withCredentials: true},
                        success: function (data) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                searchBy();
                            });
                        },
                        error: function (XMLHttpRequest, textStatus, thrown) {
                            $.messager.alert("提示", "操作失败", "error");
                        }
                    });
                }
            });
        }

        function edit(id) {
            window.parent.addTab("修改报告", "/webapp/pages/channel/upload/edit.html?id=" + id);
        }

        function manage(id) {
            window.parent.addTab("上传文件", "/webapp/pages/channel/upload/import.html?id=" + id);
        }

        function createAll() {
            $.ajax({
                url: config.url + "/dataReport/all",
                type: "post",
                data: {},
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 50,
                        text: "数据加载中..."
                    });
                },
                success: function (data) {
                    $.messager.progress("close");
                    $.messager.alert("提示", "操作成功", "info", function () {
                        searchBy();
                    });

                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }

        function searchBy(page, size) {
            var data = getFormData(document.getElementById("ff"));
            data['page'] = page || 1;
            data['size'] = $("#datagrid").datagrid("getPager").pagination("options").pageSize||100;
            data.reportType = window.parent.workMode.toUpperCase();
            data.countFlag = $("#countFlag").switchbutton("options").checked;
            if (isMonthType()) {
                if (!data.reportMonth) {
                    data.reportMonth = window.parent.timeData.monthInYear;
                }
            } else {
                if (!data.reportWeek) {
                    data.reportWeek = window.parent.timeData.weekInYear;
                }
            }
            $.ajax({
                url: config.url + "/dataReport/page",
                type: "get",
                data: transfer(data),
                dataType: "json",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 50,
                        text: "数据加载中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (data) {
                    $.messager.progress("close");
                    if (data.code == 200) {
                        $("#datagrid").datagrid("loadData", data.msg);
                    }else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                },
                error: function (data) {
                    // console.log(data);
                    $.messager.progress("close");
                }
            });
        }

        function importFormatter(val, row) {
            var res = "";
            switch (val) {
                case "WAITING":
                    res = "<span style='color: goldenrod'>待导入(1.0)</span>";
                    break;
                case "WAITING_NEW":
                    res = "<span style='color: goldenrod'>待导入(2.0)</span>";
                    break;
                case "WAITING_NEW3":
                    res = "<span style='color: goldenrod'>待导入(3.0)</span>";
                    break;
                case "WAITING_NEW4":
                    res = "<span style='color: goldenrod'>待导入(4.0)</span>";
                    break;
                case "WAITING_AI_OLD":
                    res = "<span style='color: goldenrod'>待导入(AI旧)</span>";
                    break;
                case "WAITING_AI":
                    res = "<span style='color: goldenrod'>待识别(AI)</span>";
                    break;
                case "WAITING_JAVA":
                    res = "<span style='color: goldenrod'>待解析(java)</span>";
                    break;
                case "RUNNING":
                    res = "<span style='color: #4cae4c'>正在分析(1.0)</span>";
                    break;
                case "RUNNING_NEW":
                    res = "<span style='color: #4cae4c'>正在分析(2.0)</span>";
                    break;
                case "RUNNING_NEW3":
                    res = "<span style='color: #4cae4c'>正在分析(3.0)</span>";
                    break;
                case "RUNNING_NEW4":
                    res = "<span style='color: #4cae4c'>正在分析(4.0)</span>";
                    break;
                case "AI_DISCERN":
                    res = "<span style='color: #4cae4c'>AI正在识别</span>";
                    break;
                case "RUNNING_JAVA":
                    res = "<span style='color: #4cae4c'>解析中(Java)</span>";
                    break;
                case "AI_FAIL":
                    res = "<span style='color: red'>AI识别异常</span>";
                    break;
                case "FAIL":
                    res = "<span style='color: red'>导入失败</span>";
                    break;
                case "RETURN_FAIL":
                    res = "<span style='color: red'>分析退货失败</span>";
                    break;
                case "RETURNING":
                    res = "<span style='color: #4cae4c'>分析退货中</span>";
                    break;
                case "MANY_ERROR_INDEX":
                    res = "<span style='color: red'>异常行过多</span>";
                    break;
                case "STOP_JAVA":
                    res = "<span style='color: red'>解析AI中断</span>";
                    break;
                case "STOP":
                    res = "<span style='color: red'>上传中断</span>";
                    break;
                case "ACCESS_FAIL":
                    res = "<span style='color: red'>AI访问异常</span>";
                    break;
                case "SUCCESS":
                    res = "<a class='l-btn-left l-btn-icon-left' style='width:24px;height:24px;'><span class='l-btn-icon icon-ok'></span></a>";
                    break;
                case "NONE":
                    res = "<span style='color: #999999'>无记录</span>";
                    break;
                default:
                    res = "<span style='color: #ff0b23'>"+val+"</span>";
            }
            // console.log(res);
            return res;
        }

        function clickHandler() {
            // alert("点击确定");
            $("#uploadChannel").window("close");
            $("#taskListText").window("close");
            searchBy();
        }

        function  taskList() {
            $.ajax({
                url: config.url + "/dataReport/taskList",
                type: "GET",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 30,
                        text: "操作进行中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (result) {
                    $.messager.progress("close");
                    if (result.code == 200) {
                        var bodyHtml="";
                        if (result.data) {
                            for (let i = 0; i < result.data.length; i++) {
                                bodyHtml += "<tr>" ;
                                bodyHtml +="<td>" + (i + 1) + "</td>";
                                bodyHtml +="<td>" + (result.data[i].year ? result.data[i].year : '') + "</td>";
                                bodyHtml +="<td>" + (result.data[i].month ? result.data[i].month : '') + "</td>";
                                bodyHtml +="<td>" + (result.data[i].week ? result.data[i].week : '') + "</td>" ;
                                bodyHtml +="<td>" + result.data[i].reportName + "</td>" ;
                                bodyHtml +="<td>" + result.data[i].channelName + "</td>" ;
                                bodyHtml +="<td>" + result.data[i].fileName + "</td>" ;
                                bodyHtml +="<td>" + result.data[i].statusDesc + "</td>" ;
                                bodyHtml +="<td><a href='javascript:void(0);' class='easyui-linkbutton l-btn l-btn-small' style='width:100px;'  onclick='manage(\"" + result.data[i].dateReportId + "\")'>查看</a></td>" ;
                                bodyHtml +="</tr>";
                            }
                        }else {
                            bodyHtml = "<tr><td colspan=\"9\">暂时没有上传记录</td></tr>";
                        }
                        document.getElementById("taskListBody").innerHTML = bodyHtml;
                        $("#taskListText").window("open");
                    } else {
                        $.messager.alert("提示", result.msg, "error");
                    }
                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }
        function cq() {
            window.parent.addTab("渠道核查", "/webapp/pages/product/audit/categorymiss.html");
        }
        function cxs() {
            window.parent.addTab("渠道细审", "/webapp/pages/product/audit/mallaudit.html");
        }

        function channelStatistics() {
            var data = getFormData(document.getElementById("ff"));
            data.reportType = window.parent.workMode.toUpperCase();
            data.countFlag = $("#countFlag").switchbutton("options").checked;
            if (isMonthType()) {
                if (!data.reportMonth) {
                    data.reportMonth = window.parent.timeData.monthInYear;
                }
            } else {
                if (!data.reportWeek) {
                    data.reportWeek = window.parent.timeData.weekInYear;
                }
            }
            $.ajax({
                url: config.url + "/dataReport/channelReason",
                type: "GET",
                data: transfer(data),
                dataType: "json",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 30,
                        text: "操作进行中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (message) {
                    if (message.code == 200) {
                        document.getElementById("channelReason").innerHTML = message.msg;
                        $("#channelS").window("open");
                    } else {
                        $.messager.alert("提示", message.msg, "info");
                    }
                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }

        function upload_refresh() {
            $("#taskListText").window("close");
            //上传文件刷新
            $.ajax({
                url: config.parseUrl + "/cron/import-parse/ec44c42f91ef4478b1c46838601788cb",
                type: "GET",
                xhrFields: {withCredentials: true},
                success: function () {
                },
                error: function () {
                }
            });
            taskList();
        }
        function ai_refresh() {
            $("#taskListText").window("close");
            //发送Ai请求刷新
            $.ajax({
                url: config.url + "/cron/sendAiRequest",
                type: "GET",
                success: function () {
                },
                error: function () {
                }
            });
            taskList();
        }


        function AiReAnalysis() {
            $.messager.confirm("提示", "您确定修改所有AI异常文件为【待识别】吗?", function (r) {
                if (r) {
                    $.ajax({
                        url: config.url + "/dataReport/AiReAnalysis",
                        type: "POST",
                        dataType: "json",
                        xhrFields: {withCredentials: true},
                        success: function (data) {
                            refresh();
                        },
                        error: function () {
                            $.messager.progress("close");
                        }
                    });
                }
            })
        }

        function errorRecord() {
            $.ajax({
                url: config.url + "/dataReport/errorRowRecord",
                type: "GET",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 30,
                        text: "操作进行中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (message) {
                    if (message.code == 200) {
                        document.getElementById("errorRecord").innerHTML = message.msg;
                        $("#errorRecordText").window("open");
                    } else {
                        $.messager.alert("提示", message.msg, "info");
                    }
                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }
        //异常记录-详情
        function details(errorIndex) {
            $.messager.alert("提示", errorIndex);
        }
        //异常记录-确认
        function confirm(id) {
            $.ajax({
                url: config.url + "/dataReport/confirm/"+id,
                type: "POST",
                xhrFields: {withCredentials: true},
                beforeSend: function () {
                    $.messager.progress({
                        title: "请稍等",
                        interval: 30,
                        text: "操作进行中..."
                    });
                },
                complete: function () {
                    $.messager.progress("close");
                },
                success: function (message) {
                    $.messager.progress("close");
                    if (message.code == 200) {
                        errorRecord();
                    } else {
                        $.messager.alert("提示", message.msg, "error");
                    }
                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }

        function remarks(id) {
            $("#reasonText").window("open");
            $("#reason").textbox("setValue","");
            dId=id;
        }
        function clickReason(id) {
            $("#reason").textbox('getValue')
            var data = getFormData(document.getElementById("ff"));
            data.id=dId;
            data.remarks =  $("#reason").textbox('getValue');
            $.ajax({
                url: config.url + "/dataReport/remarksYes",
                type: "GET",
                data: transfer(data),
                dataType: "json",
                xhrFields: {withCredentials: true},
                // beforeSend: function () {
                //     $.messager.progress({
                //         title: "请稍等",
                //         interval: 30,
                //         text: "操作进行中..."
                //     });
                // },
                complete: function () {
                    $.messager.progress("close");
                    $("#reasonText").window("close");
                    channelStatistics();
                },
                success: function (message) {
                    $("#reasonText").window("close");
                    channelStatistics();
                },
                error: function () {
                    $.messager.progress("close");
                }
            })
        }
    </script>
</html>